package com.zyh.map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @Author zyh
 * @Date 2024/8/1 11:31
 * @Description Map集合的遍历
 */

public class MapDemo2 {
    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1,"zyh");
        map.put(2,"jyh");
        map.put(3,"mjd");
        map.put(4,"cxk");
        map.put(5,"dz");
        //1.根据键找值
        Set<Integer> keySet = map.keySet();
        for (Integer key : keySet) {
            String value = map.get(key);
            System.out.println(key+"---"+value);
        }
        System.out.println("------------------------");
        //迭代器方法
        Iterator<Integer> iterator = keySet.iterator();
        while (iterator.hasNext()) {
            Integer key = iterator.next();
            String value = map.get(key);
            System.out.println(key+"---"+value);
        }
        System.out.println("-------------------------");
        //2.Entry对象
        Set<Map.Entry<Integer, String>> entries = map.entrySet();
        for (Map.Entry<Integer, String> entry : entries) {
            System.out.println(entry.getKey()+"---"+entry.getValue());
        }
        System.out.println("-------------------------");
        //迭代器
        Iterator<Map.Entry<Integer, String>> iterator1 = entries.iterator();
        while (iterator1.hasNext()) {
            Map.Entry<Integer, String> entry = iterator1.next();
            System.out.println(entry.getKey()+"---"+entry.getValue());
        }
    }
}
